<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>{{ $route.query.type == "add" ? '新建专家成果' : "编辑专家成果" }}</span>
        <el-button type="primary" :loading="buttonLoading" @click="goList">退出</el-button>
      </div>
    </template>

    <div class="add-form" v-loading="loading">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
        status-icon
      >
        <div v-for="(item, index) in showFields" :key="index">
          <el-form-item :label="item.label" :prop="item.key" v-if="item.type == 'input'" :rules="[{ required: item.required, message: `请填写${item.label}`, trigger: 'blur' }]">
            <el-input v-model="ruleForm[item.key]" :placeholder="`请输入${item.label}`" :disabled="item.diabled"></el-input>
          </el-form-item>

          <el-form-item :label="item.label" :prop="item.key" v-if="item.type == 'number'" :rules="[{ required: item.required, message: `请填写${item.label}`, trigger: 'blur' }]">
            <el-input-number
              v-model="ruleForm[item.key]"
              controls-position="right"
              style="width: 100%;"
              :placeholder="`请输入${item.label}`"
              :min="0"
            />
          </el-form-item>

          <el-form-item :label="item.label" :prop="item.key" v-if="item.type == 'dateyear'" :rules="[{ required: item.required, message: `请选择${item.label}`, trigger: 'change' }]">
            <el-date-picker
              v-model="ruleForm[item.key]"
              type="year"
              value-format="YYYY"
              :placeholder="`请选择${item.label}`"
            />
          </el-form-item>

          <el-form-item :label="item.label" :prop="item.key" v-if="item.type == 'date'" :rules="[{ required: item.required, message: `请选择${item.label}`, trigger: 'change' }]">
            <el-date-picker
              v-model="ruleForm[item.key]"
              value-format="YYYY-MM-DD"
              type="date"
              :placeholder="`请选择${item.label}`"
            />
          </el-form-item>

          <el-form-item :label="item.label" :prop="item.key" v-if="item.type == 'textarea'" :rules="[{ required: item.required, message: `请填写${item.label}`, trigger: 'blur' }]">
            <el-input v-model="ruleForm[item.key]" :placeholder="`请输入${item.label}`" type="textarea" autosize></el-input>
          </el-form-item>

          <el-form-item :label="item.label" :prop="item.key" v-if="item.type == 'editor'" :rules="[{ required: item.required, message: `请填写${item.label}`, trigger: 'change' }]">
            <t-editor v-model="ruleForm[item.key]" @input="inputContent" style="width: 100%;"/>
          </el-form-item>
        </div>
        
        <el-form-item class="add-button">
          <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="buttonLoading">
            {{ $route.query.type == "add" ? '新增' : "编辑" }}
          </el-button>
          <el-button @click="resetForm(ruleFormRef)" :loading="buttonLoading">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
export default { name: "backendExpertAchievementsAdd" }
</script>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue';
import { useRouter } from 'vue-router'
import { UploadFilled } from '@element-plus/icons-vue'
import tEditor from "@/components/tinymceEditor/tEditor.vue"
import { ElMessage } from 'element-plus'
import { http } from '@/utils/http/index';
import { format } from '@/plugins/moment.js'
import { ContentTypeEnum } from '@/enums/httpEnum';

/* 路由 */
const route = useRouter()

onMounted(() => {
  if(route.currentRoute.value.query.checkAchievement) {
    getAchievementsField()
  }
  if(route.currentRoute.value.query.id){
    getExpertAchievementsDetail()
  }

  /* getExpertList() */
  /* 如果是从专家列表跳转过来，默认选中此专家 */
  /* if(route.currentRoute.value.query.expertId){
    ruleForm.value.expertId = route.currentRoute.value.query.expertId
  } */
})

/* 详情数据加载动画 */
const loading = ref(false)

/* 查询详情 */
const getExpertAchievementsDetail = () => {
  loading.value = true
  http.$GET("/TheoryStudyM/GetExpertAchievementsDetailById", { Id: route.currentRoute.value.query.id }).then(res => {
    loading.value = false
    if (res.isError) return
    if (!res.result) return

    /* 赋值 */
    Object.keys(ruleForm.value).forEach((key)=>{
      if(res.result[key] != null){
        ruleForm.value[key] = res.result[key];
      }
    })

    /* 处理发布日期 */
    ruleForm.value.pubDate = format(res.result.pubDate)

    /* 处理数值 */
    ruleForm.value.timesCited = Number(res.result.timesCited)
    ruleForm.value.pvCnki = Number(res.result.pvCnki)
  })
}

/* 专家下拉列表请求 */
const expertList = ref([])
const expertPageIndex = ref(1)
const expertPageSize = ref(1000)

const getExpertList = () => {
  let params = {
    pageIndex: expertPageIndex.value,
    pageSize: expertPageSize.value
  }
  http.$POST(`/TheoryStudyM/GetExpertInfoList`, params).then(res => {
    expertList.value = expertList.value.concat(res.result.dataLst)
    if(res.result.countAll > res.result.pageIndex * res.result.pageSize){
      expertPageIndex.value = expertPageIndex.value + 1
      getExpertList()
    }
  })
}

/* 新增、编辑表单 */
const ruleFormRef = ref(null)

/* 表单数据 */
const ruleForm = ref({
})

/* 富文本内容变化 */
const inputContent = (newVal) => {
  const key = showFields.value.find(item => item.type == 'editor').key
  if(newVal){
    ruleFormRef.value.clearValidate(key)
  }
  ruleForm.value[key] = newVal
}

/* 提交新增或编辑时，按钮禁用 */
const buttonLoading = ref(false)

/* 提交表单 */
const submitForm = (formName) => {
  ruleFormRef.value.validate((valid, fields) => {
    if(valid) {
      console.log(ruleForm.value)
      buttonLoading.value = true

      /* 新增时不传id */
      if(route.currentRoute.value.query.type == 'add'){
        delete ruleForm.value.id
      }

      let requestUrl = ""
      const type = route.currentRoute.value.query.checkAchievement
      if(type == 'CJFD') {
        requestUrl = '/AchievementsM/AddOrUpdateCJFD'
      } else if(type == 'CCND') {
        requestUrl = '/AchievementsM/AddOrUpdateCCND'
      } else if(type == 'CDFD') {
        requestUrl = '/AchievementsM/AddOrUpdateCDFD'
      } else if(type == 'CMFD') {
        requestUrl = '/AchievementsM/AddOrUpdateCMFD'
      } else if(type == 'CPFD') {
        requestUrl = '/AchievementsM/AddOrUpdateCPFD'
      } else if(type == 'IPFD') {
        requestUrl = '/AchievementsM/AddOrUpdateIPFD'
      } else if(type == 'SCPD') {
        requestUrl = '/AchievementsM/AddOrUpdateSCPD'
      }

      http.$POST(requestUrl, {...ruleForm.value}, { headers: { 'Content-Type': ContentTypeEnum.FORM_DATA } }).then(res => {
        buttonLoading.value = false
        if (res.isError) {
          ElMessage({
            type: 'warning',
            message: res.message,
          })
        }else{
          ElMessage({
            type: 'success',
            message: '操作成功',
          })
          route.push({
            path: "/backend/expert-achievements"
          })
        }
      }).catch(err => {
        buttonLoading.value = false
        ElMessage({
          type: 'warning',
          message: '操作失败',
        })
      })
    }else{
      ElMessage({
        type: 'warning',
        message: '请先完成必填项',
      })
    }
  })
}

/* 重置表单 */
const resetForm = (formName) => {
  ruleFormRef.value.resetFields()
}

/* 返回列表 */
const goList = () => {
  route.push({
    path: "/backend/expert-achievements",
    query: route.currentRoute.value.query.currentPage ? { currentPage: route.currentRoute.value.query.currentPage } : {}
  })
}

const showFields = ref([])
/* 不同库字段配置 */
const getAchievementsField = () => {
  const type = route.currentRoute.value.query.checkAchievement
  if(type == 'CCND') {
    showFields.value = [
      { label: '正标题', key: 'Title', type: 'input', required: true },
      { label: '作者', key: 'Author', type: 'input', required: true },
      { label: '作者单位', key: 'AuthorAffiliation', type: 'input', required: true },
      { label: '报纸中文名', key: 'ChineseNewspaperName', type: 'input', required: true },
      { label: '出版单位', key: 'PublishingUnit', type: 'input', required: true },
      { label: '全文', key: 'FullText', type: 'editor' },
      { label: '年', key: 'Year', type: 'dateyear', required: true },
      { label: '日期', key: 'PubDate', type: 'date', required: true },
      { label: '更新日期', key: 'UpdateDate', type: 'date' },
      { label: '来源数据库', key: 'SourceDatabase', type: 'input' },
      { label: '原文格式', key: 'OriginalFormat', type: 'input' },
      { label: 'FILETYPE', key: 'FileType', type: 'input' },
      { label: '下载频次', key: 'DownloadFrequency', type: 'number' },
      { label: '被引频次', key: 'CitedFrequency', type: 'number' },
      { label: '他引频次', key: 'CoCitationFrequency', type: 'number' },
      { label: 'TABLENAME', key: 'TableName', type: 'input', diabled: true },
      { label: '主题', key: 'Theme', type: 'input', required: true },
      { label: 'DBName', key: 'DBName', type: 'input' },
      { label: 'FileName', key: 'FileName', type: 'input' },
    ]
  } else if(type == 'CDFD') {
    showFields.value = [
      { label: '题名', key: 'Title', type: 'input', required: true },
      { label: '作者', key: 'Author', type: 'input', required: true },
      { label: '作者单位', key: 'AuthorAffiliation', type: 'input', required: true },
      { label: '导师', key: 'Supervisor', type: 'input', required: true },
      { label: '论文级别', key: 'ThesisLevel', type: 'input' },
      { label: '学科专业名称', key: 'DisciplineMajor', type: 'input', required: true },
      { label: '中文关键词', key: 'ChineseKeywords', type: 'input', required: true },
      { label: '中文摘要', key: 'ChineseAbstract', type: 'textarea', required: true },
      { label: '学位年度', key: 'DegreeYear', type: 'dateyear', required: true },
      { label: '全文', key: 'FullText', type: 'editor' },
      { label: '主题', key: 'Theme', type: 'input', required: true },
      { label: '来源数据库', key: 'SourceDatabase', type: 'input' },
      { label: '更新日期', key: 'UpdateDate', type: 'date' },
      { label: '原文格式', key: 'OriginalFormat', type: 'input' },
      { label: '下载频次', key: 'DownloadFrequency', type: 'number' },
      { label: '被引频次', key: 'CitedFrequency', type: 'number' },
      { label: '他引频次', key: 'CoCitationFrequency', type: 'number' },
      { label: 'FILETYPE', key: 'FileType', type: 'input' },
      { label: '中文刊名', key: 'ChineseJournalName', type: 'input', required: true },
      { label: '出版年', key: 'Year', type: 'dateyear', required: true },
      { label: '出版单位', key: 'PublishingUnit', type: 'input', required: true },
      { label: '发表时间', key: 'PubDate', type: 'date', required: true },
      { label: 'TABLENAME', key: 'TableName', type: 'input', diabled: true },
      { label: 'DBName', key: 'DBName', type: 'input' },
      { label: 'FileName', key: 'FileName', type: 'input' },
    ]
  } else if(type == 'CJFD') {
    showFields.value = [
      { label: '篇名', key: 'Title', type: 'input', required: true },
      { label: '作者', key: 'Author', type: 'input', required: true },
      { label: '机构', key: 'Organization', type: 'input', required: true },
      { label: '中文关键词', key: 'ChineseKeywords', type: 'input', required: true },
      { label: '摘要', key: 'ChineseAbstract', type: 'textarea', required: true },
      { label: '中文刊名', key: 'ChineseJournalName', type: 'input', required: true },
      { label: '年', key: 'Year', type: 'dateyear', required: true },
      { label: '全文', key: 'FullText', type: 'editor' },
      { label: '更新日期', key: 'UpdateDate', type: 'date' },
      { label: '主题', key: 'Theme', type: 'input', required: true },
      { label: '出版日期', key: 'PubDate', type: 'date' },
      { label: '来源数据库', key: 'SourceDatabase', type: 'input' },
      { label: '原文格式', key: 'OriginalFormat', type: 'input' },
      { label: '下载频次', key: 'DownloadFrequency', type: 'number' },
      { label: '被引频次', key: 'CitedFrequency', type: 'number' },
      { label: '出版单位', key: 'PublishingUnit', type: 'input' },
      { label: 'TABLENAME', key: 'TableName', type: 'input', diabled: true },
      { label: 'DBName', key: 'DBName', type: 'input' },
      { label: 'FileName', key: 'FileName', type: 'input' },
    ]
  } else if(type == 'CMFD') {
    showFields.value = [
      { label: '中文题名', key: 'Title', type: 'input', required: true },
      { label: '作者', key: 'Author', type: 'input', required: true },
      { label: '作者单位', key: 'AuthorAffiliation', type: 'input', required: true },
      { label: '导师', key: 'Supervisor', type: 'input', required: true },
      { label: '论文级别', key: 'ThesisLevel', type: 'input', required: true },
      { label: '学科专业名称', key: 'DisciplineMajor', type: 'input', required: true },
      { label: '中文关键词', key: 'ChineseKeywords', type: 'input', required: true },
      { label: '中文摘要', key: 'ChineseAbstract', type: 'textarea', required: true },
      { label: '全文', key: 'FullText', type: 'editor' },
      { label: '主题', key: 'Theme', type: 'input', required: true },
      { label: '来源数据库', key: 'SourceDatabase', type: 'input' },
      { label: '更新日期', key: 'UpdateDate', type: 'date' },
      { label: '原文格式', key: 'OriginalFormat', type: 'input' },
      { label: '下载频次', key: 'DownloadFrequency', type: 'number' },
      { label: '被引频次', key: 'CitedFrequency', type: 'number' },
      { label: '他引频次', key: 'CoCitationFrequency', type: 'number' },
      { label: 'FILETYPE', key: 'FileType', type: 'input' },
      { label: '出版年', key: 'Year', type: 'dateyear', required: true },
      { label: '出版单位', key: 'PublishingUnit', type: 'input', required: true },
      { label: '发表时间', key: 'PubDate', type: 'date', required: true },
      { label: 'TABLENAME', key: 'TableName', type: 'input', diabled: true },
      { label: '唯一id', key: 'UniqueId', type: 'input', required: true },
      { label: 'DBName', key: 'DBName', type: 'input' },
      { label: 'FileName', key: 'FileName', type: 'input' }, 
    ]
  } else if(type == 'CPFD') {
    showFields.value = [
      { label: '论文题名', key: 'Title', type: 'input', required: true },
      { label: '论文作者', key: 'Author', type: 'input', required: true },
      { label: '论文摘要', key: 'PaperAbstract', type: 'textarea', required: true },
      { label: '论文关键词', key: 'PaperKeyWords', type: 'input', required: true },
      { label: '作者机构', key: 'AuthorAffiliation', type: 'input', required: true },
      { label: '年', key: 'Year', type: 'dateyear', required: true },
      { label: '全文', key: 'FullText', type: 'editor', required: true },
      { label: '会议名称', key: 'MeettingName', type: 'input', required: true },
      { label: '会议地点', key: 'MeettingAddress', type: 'input', required: true },
      { label: '主办单位', key: 'OrganizingInstitution', type: 'input', required: true },
      { label: '出版单位', key: 'PublishingUnit', type: 'input', required: true },
      { label: '出版日期', key: 'PublishingDate', type: 'date', required: true },
      { label: '更新日期', key: 'UpdateDate', type: 'date' },
      { label: '主题', key: 'Theme', type: 'input', required: true },
      { label: '来源数据库', key: 'SourceDatabase', type: 'input' },
      { label: '下载频次', key: 'DownloadFrequency', type: 'number' },
      { label: '被引频次', key: 'CitedFrequency', type: 'number' },
      { label: '他引频次', key: 'CoCitationFrequency', type: 'number' },
      { label: 'FILETYPE', key: 'FileType', type: 'input' },
      { label: '发表时间', key: 'PubDate', type: 'date', required: true },
      { label: 'TABLENAME', key: 'TableName', type: 'input', diabled: true },
      { label: '会议级别', key: 'MeettingLevel', type: 'input', required: true },
      { label: '会议召开时间', key: 'MeettingDate', type: 'date', required: true },
      { label: 'DBName', key: 'DBName', type: 'input' },
      { label: 'FileName', key: 'FileName', type: 'input' },
    ]
  } else if(type == 'IPFD') {
    showFields.value = [
      { label: '论文题名', key: 'Title', type: 'input', required: true },
      { label: '论文作者', key: 'Author', type: 'input', required: true },
      { label: '论文摘要', key: 'PaperAbstract', type: 'textarea', required: true },
      { label: '论文关键词', key: 'PaperKeyWords', type: 'input', required: true },
      { label: '作者机构', key: 'AuthorAffiliation', type: 'input', required: true },
      { label: '年', key: 'Year', type: 'dateyear', required: true },
      { label: '全文', key: 'FullText', type: 'editor' },
      { label: '会议名称', key: 'MeettingName', type: 'input', required: true },
      { label: '会议地点', key: 'MeettingAddress', type: 'input', required: true },
      { label: '主办单位', key: 'OrganizingInstitution', type: 'input', required: true },
      { label: '出版单位', key: 'PublishingUnit', type: 'input', required: true },
      { label: '出版日期', key: 'PublishingDate', type: 'date', required: true },
      { label: '更新日期', key: 'UpdateDate', type: 'date' },
      { label: '主题', key: 'Theme', type: 'input', required: true },
      { label: '来源数据库', key: 'SourceDatabase', type: 'input' },
      { label: '下载频次', key: 'DownloadFrequency', type: 'number' },
      { label: '被引频次', key: 'CitedFrequency', type: 'number' },
      { label: '他引频次', key: 'CoCitationFrequency', type: 'number' },
      { label: 'FILETYPE', key: 'FileType', type: 'input' },
      { label: '发表时间', key: 'PubDate', type: 'date', required: true },
      { label: 'TABLENAME', key: 'TableName', type: 'input', diabled: true },
      { label: '会议级别', key: 'MeettingLevel', type: 'input', required: true },
      { label: '会议召开时间', key: 'MeettingDate', type: 'input', required: true },
      { label: 'DBName', key: 'DBName', type: 'input' },
      { label: 'FileName', key: 'FileName', type: 'input' },
    ]
  } else if(type == 'SCPD') {
    showFields.value = [
      { label: '编号', key: 'Number', type: 'input', required: true },
      { label: '公开日', key: 'PublicDate', type: 'date', required: true },
      { label: '申请日', key: 'ApplicationDate', type: 'date', required: true },
      { label: '专利名称', key: 'Title', type: 'input', required: true },
      { label: '申请人', key: 'Author', type: 'input', required: true },
      { label: '旧机标关键词', key: 'KeyWords', type: 'input', required: true },
      { label: '发明人', key: 'Inventor', type: 'input', required: true },
      { label: '地址', key: 'Address', type: 'input', required: true },
      { label: '专利代理机构', key: 'PatentAgency', type: 'input', required: true },
      { label: '代理人', key: 'Agent', type: 'input', required: true },
      { label: '摘要', key: 'Abstract', type: 'textarea', required: true },
      { label: '年', key: 'Year', type: 'dateyear', required: true },
      { label: '来源数据库', key: 'SourceDatabase', type: 'input' },
      { label: '更新日期', key: 'UpdateDate', type: 'date' },
      { label: '申请机构', key: 'ApplicationInstitution', type: 'input', required: true },
      { label: '专利类别名称', key: 'Type', type: 'input', required: true },
      { label: '国省名称', key: 'Province', type: 'input', required: true },
      { label: '主题', key: 'Theme', type: 'input', required: true },
      { label: '下载频次', key: 'DownloadFrequency', type: 'number' },
      { label: '被引频次', key: 'CitedFrequency', type: 'number' },
      { label: '他引频次', key: 'CoCitationFrequency', type: 'number' },
      { label: '文献来源', key: 'Source', type: 'input' },
      { label: '发表时间', key: 'PubDate', type: 'date', required: true },
      { label: '表名', key: 'TableName', type: 'input', diabled: true },
      { label: '库名', key: 'DBName', type: 'input' },
      { label: '文件名', key: 'FileName', type: 'input' },
    ]
  }

  showFields.value.forEach(item => {
    if(item.type == 'number') {
      ruleForm.value[item.key] = null
    } else {
      ruleForm.value[item.key] = ""
    }

    if(item.key == 'TableName') {
      ruleForm.value[item.key] = type
    }
  })
  ruleForm.value.id = ""
}
</script>

<style lang='scss' scoped>
@import "@/assets/styles/backendElement.scss";
</style>